package com.jz.community.moduleshopping.cardGoods.ui;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.cardGoods.adapter.CardClassifyGoodsListAdapter;
import com.jz.community.moduleshopping.cardGoods.info.CardCategoryInfo;
import com.jz.community.moduleshopping.cardGoods.task.GetCardCategoryGoodsTask;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/8/17 13:37
 */
public class CardClassifyGoodsActivity extends BaseMvpActivity implements OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R2.id.title_back_left)
    ImageView titleBackLeft;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.title_right)
    TextView titleRight;
    @BindView(R2.id.title_right_iv)
    ImageView titleRightIv;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R2.id.card_classify_goods_recyclerView)
    RecyclerView cardClassifyGoodsRecyclerView;
    @BindView(R2.id.card_classify_goods_refresh)
    SmartRefreshLayout cardClassifyGoodsRefresh;

    private CardClassifyGoodsListAdapter cardClassifyGoodsListAdapter;
    private int page = 0;//页数
    private int pageSize = 10;
    private CardCategoryInfo cardCategoryInfo;
    private String showCategoryId;

    @Override
    protected int getContentView() {
        return R.layout.module_shopping_card_classify_goods_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        String name = getIntent().getStringExtra("titleName");
        showCategoryId = getIntent().getStringExtra("showCategoryId");
        titleName.setText(name);
        setImmersionBar(titleToolbar);
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        getCardCategoryInfo(false);
        handleCardBrandGoodsAdapter();
    }


    @Override
    protected void addListener() {
        super.addListener();
        titleBackLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    private void getCardCategoryInfo(boolean isRefresh) {
        new GetCardCategoryGoodsTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                cardCategoryInfo = (CardCategoryInfo) o;
                if (!Preconditions.isNullOrEmpty(cardCategoryInfo) && !Preconditions.isNullOrEmpty(cardCategoryInfo.get_embedded())) {
                    cardClassifyGoodsRefresh.finishRefresh();
                    setData(isRefresh, cardCategoryInfo.get_embedded().getLinkedHashMaps());
                }else {
                    cardClassifyGoodsListAdapter.setEmptyView(noDataView(cardClassifyGoodsRecyclerView, "暂无分类商品哦！"));
                }
            }
        }).execute(showCategoryId, page + "", pageSize + "");
    }

    private void handleCardBrandGoodsAdapter() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        cardClassifyGoodsRecyclerView.setLayoutManager(gridLayoutManager);
        cardClassifyGoodsRefresh.setOnRefreshListener(this);
        cardClassifyGoodsListAdapter = new CardClassifyGoodsListAdapter(new ArrayList<>());
        cardClassifyGoodsListAdapter.setEnableLoadMore(false);
        cardClassifyGoodsListAdapter.setOnLoadMoreListener(this, cardClassifyGoodsRecyclerView);
        cardClassifyGoodsRecyclerView.setAdapter(cardClassifyGoodsListAdapter);
        cardClassifyGoodsListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance()
                        .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                        .withString("url", cardClassifyGoodsListAdapter.getData().get(position).get_links().getSelf().getHref())
                        .withInt("goodFrom", 0)
                        .navigation();
            }
        });
    }

    private void setData(boolean isRefresh, List data) {

        page++;

        if (isRefresh) {
            cardClassifyGoodsListAdapter.setNewData(data);
        } else {
            cardClassifyGoodsListAdapter.addData(data);
        }

        if (cardCategoryInfo.getPage().getTotalPages() <= page) {
            cardClassifyGoodsListAdapter.loadMoreEnd();
        } else {
            cardClassifyGoodsListAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        getCardCategoryInfo(false);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        page = 0;
        getCardCategoryInfo(true);
    }
}
